package me.ling.findAny;

import me.ling.entity.Person;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class NowJava8 {

    public static void main(String[] args) {

        List<Person> persons = Arrays.asList(
                new Person("ricky", 30),
                new Person("jack", 20),
                new Person("lawrence", 40)
        );

        Person result1 = persons.stream()                        // Convert to steam
                .filter(x -> "jack".equals(x.getName()))        // we want "jack" only
                .findAny()                                      // If 'findAny' then return found
                .orElse(null);                                  // If not found, return null

        System.out.println(result1);

        Person result2 = persons.stream()
                .filter(x -> "ahmook".equals(x.getName()))
                .findAny()
                .orElse(null);

        System.out.println(result2);

        // 多种条件
        Person result = persons.stream().filter(x -> {
            if (!"ricky".equals(x.getName()) & x.getAge() > 20) {
                return true;
            } else {
                return false;
            }

        }).findAny().orElse(null);


        // map
        String name = persons.stream()
                .filter(x -> "jack".equals(x.getName()))
                .map(Person::getName)
                .findAny()
                .orElse("");


        // List to Map
        persons.stream().collect(
                Collectors.toMap(x -> x.getName(), y -> y.getName()));
    }

}
